using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class ChatAppMsgLinkItem : BaseChatElement
	{
		private const string TAG = "ChatAppMsgLinkItem";
		private Button ContentButton;
		private MenuItem ContentMenuItem;
		private Button fromButton;
		private Image thumbImg;
		private HttpPicItem fromIcon;
		private DynamicTextBlock descriptionTxt;
		private string iconUrl;
		private AppMsgInfo mMsginfo;
		public static DependencyProperty ThumbImgSourceProperty = DependencyProperty.Register("thumbImgSource", typeof(ImageSource), typeof(ChatAppMsgLinkItem), new PropertyMetadata(new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/appmsg/default.png") as ImageSource, new PropertyChangedCallback(ChatAppMsgLinkItem.OnThumbImgSourceChanged)));
		public static DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(ChatAppMsgLinkItem), new PropertyMetadata(new PropertyChangedCallback(ChatAppMsgLinkItem.OnTitleChanged)));
		public static DependencyProperty DescriptionProperty = DependencyProperty.Register("Description", typeof(string), typeof(ChatAppMsgLinkItem), new PropertyMetadata(new PropertyChangedCallback(ChatAppMsgLinkItem.OnDescriptionChanged)));
		public static DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(string), typeof(ChatAppMsgLinkItem), new PropertyMetadata(new PropertyChangedCallback(ChatAppMsgLinkItem.OnFromChanged)));
		private bool isAppInfoInit;
		private EventWatcher mWatcher;
		public ImageSource ThumbImgSource
		{
			get
			{
				return (ImageSource)base.GetValue(ChatAppMsgLinkItem.ThumbImgSourceProperty);
			}
			set
			{
				base.SetValue(ChatAppMsgLinkItem.ThumbImgSourceProperty, value);
			}
		}
		public string Title
		{
			get
			{
				return (string)base.GetValue(ChatAppMsgLinkItem.TitleProperty);
			}
			set
			{
				base.SetValue(ChatAppMsgLinkItem.TitleProperty, value);
			}
		}
		public string Description
		{
			get
			{
				return (string)base.GetValue(ChatAppMsgLinkItem.DescriptionProperty);
			}
			set
			{
				base.SetValue(ChatAppMsgLinkItem.DescriptionProperty, value);
			}
		}
		public string From
		{
			get
			{
				return (string)base.GetValue(ChatAppMsgLinkItem.FromProperty);
			}
			set
			{
				base.SetValue(ChatAppMsgLinkItem.FromProperty, value);
			}
		}
		public ChatAppMsgLinkItem()
		{
			base.add_Loaded(new RoutedEventHandler(this.ChatAppMsgLinkItem_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.ChatAppMsgLinkItem_Unloaded));
		}
		private void ChatAppMsgLinkItem_Unloaded(object sender, RoutedEventArgs e)
		{
			this.GetAppInfoUnit();
		}
		private void ChatAppMsgLinkItem_Loaded(object sender, RoutedEventArgs e)
		{
			this.GetAppInfoInit();
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.fromButton = (base.GetTemplateChild("fromButton") as Button);
			this.fromButton.add_Click(new RoutedEventHandler(this.fromButton_Click));
			this.fromIcon = (base.GetTemplateChild("picFromIcon") as HttpPicItem);
			this.descriptionTxt = (base.GetTemplateChild("DescriptionTxt") as DynamicTextBlock);
			if (!string.IsNullOrEmpty(this.From))
			{
				this.fromButton.set_Visibility(0);
				if (!string.IsNullOrEmpty(this.iconUrl) && this.fromIcon != null)
				{
					this.fromIcon.LoadImage(this.iconUrl, true);
				}
			}
			else
			{
				this.fromButton.set_Visibility(1);
			}
			this.thumbImg = (base.GetTemplateChild("thumb") as Image);
			if (this.thumbImg != null && this.thumbImg.get_Source() != null)
			{
				this.thumbImg.set_Visibility(0);
				this.descriptionTxt.set_MaxWidth(178.0);
			}
			else
			{
				if (this.thumbImg != null)
				{
					this.thumbImg.set_Visibility(1);
				}
				this.descriptionTxt.set_MaxWidth(298.0);
			}
			this.ContentButton = (base.GetTemplateChild("ContentButton") as Button);
			this.ContentButton.SetValue(MMTiltEffect.IsTiltEnabledProperty, true);
			this.ContentButton.add_Click(new RoutedEventHandler(this.ContentButton_Click));
		}
		private void fromButton_Click(object sender, RoutedEventArgs e)
		{
			string strUrl = string.Concat(new string[]
			{
				"http://rd.wechatapp.com/app/source/?appid=",
				this.mMsginfo.appid,
				";version=24;lang=",
				R.GetCurrentLanguage(),
				";devicetype=wp7"
			});
			ToolFunction.GotoWebLinkInWebView(strUrl, GetA8KeyScene.MMGETA8KEY_SCENE_UNKNOW);
		}
		private void ContentButton_Click(object sender, RoutedEventArgs e)
		{
			if (this.mMsginfo.type != 5)
			{
				return;
			}
			ToolFunction.GotoWebLinkInWebView(this.mMsginfo.string_0, GetA8KeyScene.MMGETA8KEY_SCENE_UNKNOW);
		}
		public override void SetChatMsg(ChatMsg Msg)
		{
			base.SetChatMsg(Msg);
			if (this.chatMsg_0.IsAppMsg())
			{
				this.mMsginfo = AppMsgMgr.parseAppXml(Msg.strMsg);
				if (this.mMsginfo == null)
				{
					this.mMsginfo = new AppMsgInfo();
					this.mMsginfo.description = strings.Chat_msg_err;
				}
			}
			this.RefreshItem(Msg);
		}
		private void RefreshItem(ChatMsg chatMsg_1)
		{
			if (chatMsg_1 == null)
			{
				return;
			}
			string text = "";
			string text2 = "";
			if (!ChatMsgMgr.getMsgUser(chatMsg_1, ref text, ref text2))
			{
				Log.smethod_1("ChatAppMsgLinkItem", "getMsgUser error:" + text2);
				return;
			}
			if (this.mMsginfo == null)
			{
				base.set_Height(0.0);
				base.set_Visibility(1);
				return;
			}
			this.Title = this.mMsginfo.title;
			this.Description = this.mMsginfo.description;
			string msgThumbnail = ChatMsgMgr.getMsgThumbnail(chatMsg_1);
			if (msgThumbnail != null)
			{
				BitmapImage thumbImgSource = ImageCacheMgr.loadImage(ChatMsgMgr.getMsgThumbnail(chatMsg_1), ImageCacheType.MsgImg);
				this.ThumbImgSource = thumbImgSource;
			}
			AppInfo appinfo = AppInfoMgr.getAppinfo(this.mMsginfo.appid, MMAppInfoIconType.MMAPPINFO_ICONTYPE_SD);
			if (appinfo != null && !string.IsNullOrEmpty(appinfo.AppName))
			{
				this.From = strings.Con_QQComeFrom + appinfo.AppName;
				this.iconUrl = appinfo.AppWatermarkUrl;
				if (!string.IsNullOrEmpty(this.iconUrl) && this.fromIcon != null)
				{
					this.fromIcon.LoadImage(this.iconUrl, true);
				}
			}
		}
		private static void OnThumbImgSourceChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnTitleChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnDescriptionChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private static void OnFromChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private void GetAppInfoInit()
		{
			if (this.isAppInfoInit)
			{
				return;
			}
			this.mWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandler));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GET_APPINFO_SUCCESS, this.mWatcher);
			this.isAppInfoInit = true;
		}
		private void GetAppInfoUnit()
		{
			if (!this.isAppInfoInit)
			{
				return;
			}
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_GET_APPINFO_SUCCESS, this.mWatcher);
			this.isAppInfoInit = false;
		}
		private void onEventHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			AppInfo appInfo = evtArgs.mObject as AppInfo;
			if (appInfo == null)
			{
				return;
			}
			if (this.mMsginfo == null)
			{
				return;
			}
			if (this.mMsginfo.appid != appInfo.AppID)
			{
				return;
			}
			if (string.IsNullOrEmpty(this.mMsginfo.fromUserName))
			{
				return;
			}
			if (!string.IsNullOrEmpty(appInfo.AppName))
			{
				this.From = strings.Con_QQComeFrom + appInfo.AppName;
				this.fromButton.set_Visibility(0);
				this.iconUrl = appInfo.AppWatermarkUrl;
				if (!string.IsNullOrEmpty(this.iconUrl) && this.fromIcon != null)
				{
					this.fromIcon.LoadImage(this.iconUrl, true);
				}
			}
		}
	}
}
